package com.ocom.company.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.ocom.common.constant.CompanyServiceUrl;
import com.ocom.common.entity.company.EntityFlowConfig;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.company.GetFlowListByComRequest;
import com.ocom.common.request.company.NewComFlowRequest;
import com.ocom.common.request.company.RemoveComFlowRequest;
import com.ocom.common.request.company.UpdateComFlowRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.company.service.FlowConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class FlowConfigController {

    @Autowired
    private FlowConfigService flowConfigService;

    @PostMapping(CompanyServiceUrl.URL_newComFlow)
    public Result newComFlow(@RequestBody NewComFlowRequest newComFlowRequest){
        EntityFlowConfig entityFlowConfig = Convert.convert(EntityFlowConfig.class, newComFlowRequest);
        if(ObjectUtil.isEmpty(entityFlowConfig.getSubscript())){
            entityFlowConfig.setApplyKey(String.format("%s-%s",entityFlowConfig.getFlowKey(),entityFlowConfig.getComId()));
        }else{
            entityFlowConfig.setApplyKey(String.format("%s-%s-%s",entityFlowConfig.getFlowKey(),entityFlowConfig.getSubscript(),entityFlowConfig.getComId()));
        }
        EntityFlowConfig entityFlowConfigA = this.flowConfigService.getOne(entityFlowConfig.getComId(),entityFlowConfig);
        if(ObjectUtil.isNotEmpty(entityFlowConfigA)){
            return ResultUtil.error(ResultEnum.Err_5003);
        }
        boolean result = this.flowConfigService.save(entityFlowConfig.getComId(),entityFlowConfig);
        return ResultUtil.success(result);
    }

    @PostMapping(CompanyServiceUrl.URL_updateComFlow)
    public Result updateComFlow(@RequestBody UpdateComFlowRequest updateComFlowRequest){
        EntityFlowConfig entityFlowConfig = Convert.convert(EntityFlowConfig.class,updateComFlowRequest);
        return ResultUtil.success(this.flowConfigService.update(entityFlowConfig.getComId(),entityFlowConfig));
    }

    @PostMapping(CompanyServiceUrl.URL_removeComFlow)
    public Result removeComFlow(@RequestBody RemoveComFlowRequest removeComFlowRequest){
        EntityFlowConfig entityFlowConfig = Convert.convert(EntityFlowConfig.class,removeComFlowRequest);
        return ResultUtil.success(this.flowConfigService.remove(removeComFlowRequest.getComId(), entityFlowConfig));
    }

    @PostMapping(CompanyServiceUrl.URL_getFlowListByCom)
    public Result<EntityFlowConfig> getFlowListByCom(@RequestBody GetFlowListByComRequest getFlowListByComRequest){
        return ResultUtil.success(this.flowConfigService.getInfoDefKey(getFlowListByComRequest.getComId(),getFlowListByComRequest.getDefKey()));
    }
}
